#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
#include <sys/wait.h>
using namespace std;

#define FIFO_FILE "myfifo"

int main()
{
    pid_t pid = fork();
    if (pid > 0)
    {
        // 父进程
        int n = mkfifo(FIFO_FILE, 0666);
        if (n < 0)
        {
            perror("mkfifo fail");
            exit(1);
        }

        int fd = open(FIFO_FILE, O_WRONLY);
        if (fd < 0)
        {
            perror("open fail");
            exit(1);
        }

        const char* message = "i am process A";
        write(fd, message, strlen(message));

        close(fd);
        wait(NULL);
    }
    else if (pid == 0)
    {
        // 子进程
        int fd = open(FIFO_FILE, O_RDONLY);
        if (fd < 0)
        {
            perror("open fail");
            exit(1);
        }

        char buffer[512];
        read(fd, buffer, sizeof(buffer));

        cout << buffer << endl;
        close(fd);
    }
    else
    {
        perror("fork fail");
        exit(1);
    }

    return 0;
}